package kz.pompei.ifacer.grot;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import kz.pompei.ifacer.model.widgets.Widget;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class FileReader {
  public static List<Widget> parseFile(InputStream inputStream) throws Exception {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    WidgetHandler wh = new WidgetHandler();
    reader.setContentHandler(wh);
    reader.parse(new InputSource(inputStream));
    return wh.list;
  }
  
  private static class Position {
    final List<String> path = new ArrayList<>();
    
    String last() {
      return path.get(path.size() - 1);
    }
    
    int size() {
      return path.size();
    }
    
    String full() {
      return first(path.size());
    }
    
    String first(int n) {
      StringBuilder ret = new StringBuilder();
      int index = 0;
      for (String s : path) {
        ret.append('/').append(s);
        if (++index > n) break;
      }
      return ret.toString();
    }
    
    String pop() {
      String ret = last();
      path.remove(path.size() - 1);
      return ret;
    }
  }
  
  private static class WidgetHandler extends DefaultHandler {
    final List<Widget> list = new ArrayList<>();
    final Position position = new Position();
    final StringBuilder text = new StringBuilder();
    Attributes lastAttr = null;
    Widget widget = null;
    boolean valueRead;
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
      text.append(ch, start, length);
    }
    
    @Override
    public void startElement(String uri, String localName, String name, Attributes atts)
        throws SAXException {
      position.path.add(name);
      lastAttr = atts;
      text.setLength(0);
      beginElement();
    }
    
    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {
      completeElement();
      
      if (!name.equals(position.pop())) throw new IllegalStateException();
    }
    
    private void beginElement() {
      if ("/ifacer/widgets/widget".equals(position.full())) {
        widget = ReaderUtil.createWidget(lastAttr.getValue("type"), lastAttr.getValue("uuid"));
        return;
      }
      if (widget == null) return;
      if (position.size() == 4) {
        valueRead = lastAttr.getIndex("v") > -1;
        if (valueRead) {
          ReaderUtil.setField(widget, position.last(), lastAttr.getValue("v"));
        }
        return;
      }
    }
    
    private void completeElement() {
      if ("/ifacer/widgets/widget".equals(position.full())) {
        list.add(widget);
        widget = null;
        return;
      }
      if (position.size() == 4 && !valueRead) {
        ReaderUtil.setField(widget, position.last(), text.toString());
        return;
      }
    }
  }
}
